package com.zr.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.RemoveUsersDao;
import com.zr.model.Clog;
import com.zr.model.Cuser;

/**
 * 
 * @author HC
 *
 * @param <T>
 */
@Repository("removeUsersDao")
public class RemoveUsersDaoImpl<T> extends BaseDaoImpl<T> implements RemoveUsersDao<T> {

	@Override
	public void removeUsersByUid(int uid) {
		String sql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(sql);
		que.setInteger("uid", uid);
		List<Cuser> us = que.list();
		Cuser u = us.get(0);
		u.setUstate("已注销");
		this.getCurrentSession().save(u);
	}

	@Override
	public void insertInfoIntoLog(String time, String cuname, int cuid, String info) {
		Clog clog = new Clog();
		clog.setLinfo(info);
		clog.setLtime(time);
		clog.setUname(cuname);
		clog.setUid(cuid);
		this.getCurrentSession().save(clog);
	}

	@Override
	public int getRemovedOrNot(int uid) {
		int i = 0;
		String sql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(sql);
		que.setInteger("uid", uid);
		List<Cuser> us = que.list();
		Cuser u = us.get(0);
		String state = "已注销";
		if (state.equals(u.getUstate())) {
			i=2;
		} else {
			u.setUstate("已注销");
			this.getCurrentSession().save(u);
            i=1;
		}
		return i;
	}

}
